﻿Public Class Sorter
    Dim data As New List(Of NumericUpDown)

    Private Sub Sorter_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'MsgBox(SortingPanel1.State.ToString())
    End Sub

    Private Function pre_run()
        Dim status = True

        If cbOrder.SelectedIndex >= 0 And cbAlgorithm.SelectedIndex >= 0 And cbSpeed.SelectedIndex >= 0 Then
            SortingPanel1.Order = cbOrder.SelectedIndex
            SortingPanel1.Algorithm = cbAlgorithm.SelectedIndex
            SortingPanel1.Speed = cbSpeed.SelectedIndex
        Else
            status = False
            MsgBox("Please set all settings!")
        End If

        Return status
    End Function

    Private Function init()
        Dim status = True

        If txtInput.Lines.Count > 1 Then
            If txtInput.Lines.Count <= 8 Then
                SortingPanel1.Items.Clear()

                Dim allInteger = True
                Try
                    For i = 0 To txtInput.Lines.Count - 1
                        Dim res = CType(txtInput.Lines(i).ToString, Integer)
                    Next
                Catch ex As Exception
                    allInteger = False
                End Try

                If allInteger Then
                    For i = 0 To txtInput.Lines.Count - 1
                        SortingPanel1.Items.Add(CType(txtInput.Lines(i).ToString, Integer))
                    Next
                Else
                    status = False
                    MsgBox("All data must be integer!")
                End If
            Else
                status = False
                MsgBox("A maximum number of only 8 data can be stored!")
            End If
        Else
            status = False
            MsgBox("Please insert data!")
        End If

        Return status
    End Function

    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        Try
            If pre_run() Then
                SortingPanel1.StartAnimation()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        Try
            SortingPanel1.StopAnimation()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnContinue_Click(sender As Object, e As EventArgs) Handles btnContinue.Click
        Try
            SortingPanel1.ContinueAnimation()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnPause_Click(sender As Object, e As EventArgs) Handles btnPause.Click
        Try
            SortingPanel1.PauseAnimation()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnInitialize_Click(sender As Object, e As EventArgs) Handles btnInitialize.Click
        Try
            If init() Then
                SortingPanel1.InitializeAnimation()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        Try
            SortingPanel1.Reset()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnGetResult_Click(sender As Object, e As EventArgs) Handles btnGetResult.Click
        Dim out As String = ""
        For i = 0 To SortingPanel1.Count - 1
            out &= SortingPanel1.Items(i) & Space(1)
        Next
        MsgBox(out)
    End Sub
End Class
